package com.gly091020.GLYCan;

import com.gly091020.GLYCan.block.CanBlock;
import com.gly091020.GLYCan.block.entity.CanBlockEntity;
import com.gly091020.GLYCan.item.CanItem;
import com.gly091020.GLYCan.util.CanComponent;
import com.mojang.logging.LogUtils;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.core.component.DataComponentType;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.world.food.FoodProperties;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.CreativeModeTabs;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.EventBusSubscriber;
import net.neoforged.fml.common.Mod;
import net.neoforged.neoforge.event.BuildCreativeModeTabContentsEvent;
import net.neoforged.neoforge.registries.DeferredHolder;
import net.neoforged.neoforge.registries.DeferredRegister;
import org.slf4j.Logger;

import java.util.function.Supplier;

@Mod(GLYCanMod.ModID)
public class GLYCanMod {
    public static final String ModID = "gly_can";
    private static final Logger LOGGER = LogUtils.getLogger();

    public static final DeferredRegister<Item> ITEMS = DeferredRegister.create(BuiltInRegistries.ITEM, ModID);
    public static final DeferredRegister<Block> BLOCKS = DeferredRegister.createBlocks(ModID);
    public static final DeferredRegister<BlockEntityType<?>> BLOCK_ENTITY_TYPES = DeferredRegister.create(Registries.BLOCK_ENTITY_TYPE, ModID);
    public static final DeferredRegister<SoundEvent> SOUND_EVENTS =
            DeferredRegister.create(BuiltInRegistries.SOUND_EVENT, ModID);
    public static final DeferredRegister.DataComponents DATA_COMPONENTS = DeferredRegister.createDataComponents(Registries.DATA_COMPONENT_TYPE, ModID);

    public static final DeferredHolder<SoundEvent, SoundEvent> TETO_CAN_SOUND = SOUND_EVENTS.register(
            "teto_can",
            () -> SoundEvent.createVariableRangeEvent(ResourceLocation.fromNamespaceAndPath(ModID, "teto_can"))
    );
    public static final DeferredHolder<Block, CanBlock> CAN_BLOCK = BLOCKS.register("can_block", resourceLocation -> new CanBlock(BlockBehaviour.Properties.of()
            .noOcclusion().lightLevel((state)->0)));
    public static final DeferredHolder<Item, CanItem>
            CAN = ITEMS.register("can", () -> new CanItem(GLYCanMod.CAN_BLOCK.get(), new Item.Properties().stacksTo(1).food(new FoodProperties.Builder().build())));
    public static final Supplier<BlockEntityType<CanBlockEntity>> CAN_BLOCK_ENTITY = BLOCK_ENTITY_TYPES.register("can_block_entity", () -> BlockEntityType.Builder.of(
            CanBlockEntity::new, CAN_BLOCK.get()
    ).build(null));
    public static final DeferredHolder<DataComponentType<?>, DataComponentType<CanComponent>> CAN_COMPONENTS = DATA_COMPONENTS.registerComponentType("can_data", builder -> builder.persistent(RecordCodecBuilder.create(instance -> instance.group(Codec.BOOL.fieldOf("is_teto").forGetter(CanComponent::isTeto), Codec.INT.fieldOf("color").forGetter(CanComponent::color)).apply(instance, CanComponent::new))));

    public GLYCanMod(IEventBus modBus){
        ITEMS.register(modBus);
        BLOCKS.register(modBus);
        BLOCK_ENTITY_TYPES.register(modBus);
        SOUND_EVENTS.register(modBus);
        DATA_COMPONENTS.register(modBus);
    }
}

@EventBusSubscriber
class EventHandler{
    @SubscribeEvent
    public static void buildContents(BuildCreativeModeTabContentsEvent event) {
        if(event.getTabKey() == CreativeModeTabs.FOOD_AND_DRINKS){
            event.insertFirst(new ItemStack(GLYCanMod.CAN.get()), CreativeModeTab.TabVisibility.PARENT_AND_SEARCH_TABS);
        }
    }
}
